local sk__kuizhu = fk.CreateSkill {

  name = "sk__kuizhu",

  tags = { Skill.Compulsory, },

}



sk__kuizhu:addEffect(fk.EventPhaseStart, {
  name = "sk__kuizhu",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__kuizhu.name) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__kuizhu.name)
    local n1, n2 = 0, 0
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from and move.moveReason == fk.ReasonDiscard then
          if move.from == player then
            n2 = n2 + #move.moveInfo
          else
            n1 = n1 + #move.moveInfo
          end
        end
        return false
      end
    end, Player.HistoryTurn)
    if n1 > n2 then
      room:notifySkillInvoked(player, sk__kuizhu.name, "offensive")
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, 
        "#sk__kuizhu-choose", sk__kuizhu.name, false)
      room:damage({
        from = player,
        to = room:getPlayerById(to[1]),
        damage = 2,
        skillName = sk__kuizhu.name,
      })
    else
      room:notifySkillInvoked(player, sk__kuizhu.name, "drawcard")
      room:loseHp(player, 1, sk__kuizhu.name)
      if not player.dead then
        player:drawCards(#room.alive_players, sk__kuizhu.name)
      end
    end
  end,
})

return sk__kuizhu